home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d879.lha / DiskTest / Source / beginend.c next >
C/C++ Source or Header  |  1993-04-04  |  14KB  |  439 lines

  1. /*---------------------------------------------------------*
  2.  | File: BEGINEND.c - Initialisation, and Cleanup routines |
  3.  +---------------------------------------------------------+
  4.  | Author:  Maurizio Loreti, aka MLO or I3NOO.             |
  5.  | Address: University of Padova - Department of Physics   |
  6.  |          Via F. Marzolo, 8 - 35131 PADOVA - Italy       |
  7.  | Phone:   (39)(49) 844-313         FAX: (39)(49) 844-245 |
  8.  | E-Mail:  loreti@padova.infn.it (TCP/IP)                 |
  9.  | Home: Via G. Donizetti 6 - 35010 CADONEGHE (PD) - Italy |
  10.  *---------------------------------------------------------*/
  11.  
  12. /**
  13.  | #includes
  14. **/
  15.  
  16. #include <stddef.h>                       /* Standard library */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <exec/types.h>                   /* Amiga specific */
  21. #include <exec/memory.h>
  22. #include <intuition/gadgetclass.h>
  23. #include <dos/dos.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/graphics_protos.h>
  26. #include <clib/intuition_protos.h>
  27. #include <clib/gadtools_protos.h>
  28. #include <clib/asl_protos.h>
  29. #include <clib/alib_protos.h>
  30. #include <clib/wb_protos.h>
  31. #include <clib/dos_protos.h>
  32. #include "main.h"                         /* Local stuff */
  33. #include "beginend.h"
  34. #include "ext.h"
  35.  
  36. void Cleanup(void)
  37. {
  38. /**
  39.  | This routines releases all allocated resources before
  40.  | exiting to the operating system.
  41.  | Since the program detaches itself from the CLI, we don't
  42.  | need to return an error code if something strange has been
  43.  | detected: the return code is always "EXIT_OK".
  44.  |
  45.  | - frees all memory obtained from malloc() or AllocMem();
  46.  | - frees the IOrequest and deletes the port of the trackdisk.device;
  47.  | - closes the "break" window;
  48.  | - closes the AppWindow stuff;
  49.  | - closes the output window;
  50.  | - releases the ASL file/directory requester;
  51.  | - releases every resource needed for the gadtools gadgets, and
  52.  |   unlocks the Workbench screen;
  53.  | - closes all the opened libraries:
  54.  | - restore the process' pr_WindowPtr.
  55. **/
  56.  
  57.   if (pClearRP   != NULL)     free(pClearRP);
  58.   if (diskBuffer != NULL)     FreeMem(diskBuffer, diskBufferLength);
  59.   if (pFIB       != NULL)     FreeMem(pFIB, sizeof(struct FileInfoBlock));
  60.   ClearText(FALSE);
  61.  
  62.   if (diskReq != NULL) {
  63.     CloseDevice((struct IORequest *) diskReq);
  64.     DeleteExtIO((struct IORequest *) diskReq);
  65.   }
  66.   if (diskPort != NULL)       DeletePort(diskPort);
  67.  
  68.   if (pBWind != NULL) {
  69.     struct IntuiMessage *pIM;
  70.  
  71.     while ((pIM = GT_GetIMsg(pBWind->UserPort)) != NULL) {
  72.       GT_ReplyIMsg(pIM);
  73.     }
  74.     CloseWindow(pBWind);
  75.   }
  76.   if (pBGad != NULL) FreeGadgets(pBGad);
  77.  
  78.   if (pAWind != NULL) {
  79.     struct Message *pM;
  80.  
  81.     while ((pM = GetMsg(appWinPort)) != NULL) {
  82.       ReplyMsg(pM);
  83.     }
  84.     (void) RemoveAppWindow(pAWind);
  85.   }
  86.   if (appWinPort != NULL)     DeletePort(appWinPort);
  87.  
  88.   if (pWind != NULL) {
  89.     struct IntuiMessage *pIM;
  90.  
  91.     while ((pIM = GT_GetIMsg(pWind->UserPort)) != NULL) {
  92.       GT_ReplyIMsg(pIM);
  93.     }
  94.     ClearMenuStrip(pWind);
  95.     CloseWindow(pWind);
  96.   }
  97.  
  98.   if (pFR     != NULL)        FreeAslRequest(pFR);
  99.   if (pGlist  != NULL)        FreeGadgets(pGlist);
  100.   if (pMenu   != NULL)        FreeMenus(pMenu);
  101.   if (pVI     != NULL)        FreeVisualInfo(pVI);
  102.   if (pScr    != NULL)        UnlockPubScreen(NULL, pScr);
  103.  
  104.   if (WorkbenchBase != NULL)  CloseLibrary(WorkbenchBase);
  105.   if (GadToolsBase  != NULL)  CloseLibrary(GadToolsBase);
  106.   if (LayersBase    != NULL)  CloseLibrary(LayersBase);
  107.   if (GfxBase       != NULL)  CloseLibrary(GfxBase);
  108.   if (IntuitionBase != NULL)  CloseLibrary(IntuitionBase);
  109.  
  110.   if (mySelf != NULL) mySelf->pr_WindowPtr = save_pr_WindowPtr;
  111.  
  112.   exit(EXIT_OK);
  113. }
  114.  
  115. void Init(void)
  116. {
  117. /**
  118.  | Main initialisation routine:
  119.  | - disable system requesters caused from this process;
  120.  | - initialise the Intuition stuff;
  121.  | - fill and initialise the text scroller.
  122. **/
  123.  
  124.   mySelf = (struct Process *) FindTask(NULL);
  125.   save_pr_WindowPtr = mySelf->pr_WindowPtr;
  126.   mySelf->pr_WindowPtr = (APTR) -1L;
  127.  
  128.   IntuiInit();
  129.  
  130.   if ((pClearRP = malloc(sizeof(struct RastPort))) == NULL   ||
  131.     (pFIB = (struct FileInfoBlock *)
  132.        AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR)) == NULL) {
  133.     Error("Not enough memory");
  134.   }
  135.  
  136.   HelpScreen();
  137.   InitScroller();
  138. }
  139.  
  140. /*-------------------------- Local Procedures --------------------------*/
  141.  
  142. static void HelpScreen(void)
  143. {
  144. /**
  145.  | Fills the output scroller with a short help text,
  146.  | to be displayed at the program start.
  147. **/
  148.  
  149.   size_t i;
  150.   char *helpText[] = {
  151.     VersionTag + 7,
  152.     "",
  153.     "You can now click on a button gadget: the DF buttons start the",
  154.     "full disk test (defective tracks, and file integrity)  for the",
  155.     "selected drive,  while the QUIT button terminates the program.",
  156.     "",
  157.     "You can also drop an icon  into this window:  for file related",
  158.     "icons (ie tool or project), DT will check the integrity of the",
  159.     "corresponding file; otherwise (disk, drawer or trashcan icons)",
  160.     "DT will perform the integrity test on _all_ files found in the",
  161.     "related [root] directory, and _in_every_underlying_directory_.",
  162.     "",
  163.     "A third method are the menus, or their keyboard shortcuts."
  164.   };
  165.  
  166.   for (i=0; i<(sizeof(helpText)/sizeof(char *)); i++) {
  167.     AddLine(helpText[i], strlen(helpText[i]), FALSE);
  168.   }
  169. }
  170.  
  171. static void IntuiInit(void)
  172. {
  173. /**
  174.  | This routine performs the Intuition part of the initialisation:
  175.  | - opens all the needed libraries;
  176.  | - finds how many and which floppy units are on the system;
  177.  | - creates all the gadtools gadgets to be put in the output window;
  178.  | - creates the gadtools gadgets to be put in the "break" window;
  179.  | - opens the output window itself.
  180.  |
  181.  | If an error is detected in this phase, IntuiInit() exits calling
  182.  | Error(); the only exception is when the v37 libraries were not
  183.  | found, because Error() uses an EasyRequester (available in v37
  184.  | only); in that case IntuiInit displays an AutoRequester on the
  185.  | Workbench window (we need intuition.library, that is opened at
  186.  | every available revision) and exits calling Cleanup().
  187. **/
  188.  
  189.   short           i;
  190.   struct Gadget  *pG;
  191.   struct DosList *pDL;
  192.   ULONG           flags;
  193.   char            drive[] = "DF0";
  194.  
  195.   static char bgText[] = "Click here to Break";
  196.  
  197.   struct IntuiText bgIt = {
  198.     0, 0, 0, 0, 0, NULL, bgText, NULL
  199.   };
  200.  
  201.   static struct NewMenu nm[] = {
  202.     {NM_TITLE,  "Project",            NULL, 0, 0, NULL},
  203.     {NM_ITEM,   "About",              NULL, 0, 0, (APTR) M_ABOUT},
  204.     {NM_ITEM,   "Quit",               "Q",  0, 0, (APTR) M_QUIT},
  205.     {NM_TITLE,  "Disk Test",          NULL, 0, 0, NULL},
  206.     {NM_ITEM,   "DF0",                "0",  0, 0, (APTR) M_DF0},
  207.     {NM_ITEM,   "DF1",                "1",  0, 0, (APTR) M_DF1},
  208.     {NM_ITEM,   "DF2",                "2",  0, 0, (APTR) M_DF2},
  209.     {NM_ITEM,   "DF3",                "3",  0, 0, (APTR) M_DF3},
  210.     {NM_TITLE,  "Integrity Test",     NULL, 0, 0, NULL},
  211.     {NM_ITEM,   "Device/Directory",   "D",  0, 0, (APTR) M_DEVDIR},
  212.     {NM_ITEM,   "File",               "F",  0, 0, (APTR) M_FILE},
  213.     {NM_END,    NULL,                 NULL, 0, 0, NULL}
  214.   };
  215.  
  216. #define MENU_DF0  4                   /* nm[4] is the DF0 item */
  217.  
  218.   struct NewGadget ng = {
  219.     GAD_LEFT,  GAD_TOP,
  220.     GAD_WIDTH, GAD_HEIGHT,
  221.     NULL,                             /* Gadget text */
  222.     NULL,                             /* Gadget font */
  223.     0,                                /* Gadget ID */
  224.     PLACETEXT_IN,
  225.     NULL,                             /* GetVisualInfo() ret. val. */
  226.     NULL
  227.   };
  228.  
  229.   char *gadText[NUM_BUT] = {          /* Button gadgets text */
  230.     "DF0", "DF1", "DF2",
  231.     "DF3", "Quit"
  232.   };
  233.  
  234.   UWORD gadID[NUM_BUT] = {            /* Button gadgets ID */
  235.     BUT_1, BUT_2, BUT_3,
  236.     BUT_4, BUT_QUIT
  237.   };
  238.  
  239.   BOOL disabled[NUM_BUT] = {          /* Button gadgets state */
  240.     FALSE, FALSE, FALSE,
  241.     FALSE, FALSE
  242.   };
  243.  
  244.   WORD zoomBox[4] = {
  245.     ZB_LEFT, ZB_TOP, ZB_WIDTH, 0
  246.   };
  247.  
  248. /**
  249.  | Libraries
  250. **/
  251.  
  252.   IntuitionBase = OpenLibrary("intuition.library",  OS_MINREV);
  253.   GfxBase       = OpenLibrary("graphics.library",   OS_MINREV);
  254.   LayersBase    = OpenLibrary("layers.library",     OS_MINREV);
  255.   GadToolsBase  = OpenLibrary("gadtools.library",   OS_MINREV);
  256.   WorkbenchBase = OpenLibrary("workbench.library",  OS_MINREV);
  257.  
  258.   if (IntuitionBase == NULL || LayersBase    == NULL ||  GfxBase == NULL ||
  259.       GadToolsBase  == NULL || WorkbenchBase == NULL) {
  260.  
  261.     struct IntuiText it = {
  262.       0, 1, JAM2, AR_ITX, AR_ITY, NULL,
  263.       "DiskTest requires OS 2.04 (v37)", NULL
  264.     };
  265.     struct IntuiText ok = {
  266.       0, 1, JAM2, AR_OKX, AR_OKY, NULL, "OK", NULL
  267.     };
  268.  
  269.     if (IntuitionBase != NULL   ||
  270.         (IntuitionBase = OpenLibrary("intuition.library",  0)) != NULL) {
  271.       (void) AutoRequest(NULL, &it, NULL, &ok, 0, 0, AR_WX, AR_WY);
  272.     }
  273.     Cleanup();
  274.   }
  275.  
  276. /**
  277.  | Connected floppies
  278. **/
  279.  
  280.   flags = LDF_DEVICES | LDF_READ;
  281.   pDL = LockDosList(flags);
  282.   for (i=0; i<4; i++, (drive[2])++) {
  283.     disabled[i] = (BOOL)(FindDosEntry(pDL, drive, flags) == NULL);
  284.   }
  285.   UnLockDosList(flags);
  286.  
  287.   for (i=0; i<4; i++) {
  288.     if (disabled[i]) {
  289.       nm[MENU_DF0+i].nm_Flags = NM_ITEMDISABLED;
  290.     }
  291.   }
  292.  
  293. /**
  294.  | Allocates an ASL file/directory requester
  295. **/
  296.  
  297.   if ((pFR = AllocAslRequestTags(ASL_FileRequest,
  298.         ASL_FuncFlags,   FILF_MULTISELECT | FILF_PATGAD,
  299.         ASL_Pattern,     (ULONG) "~(#?.info)",
  300.         ASL_Dir,         "SYS:",
  301.         TAG_END)) == NULL) {
  302.     Error("Error from AllocAslrequest");
  303.   }
  304.  
  305. /**
  306.  | Gadtools preamble: screen, font, VisualInfo, offsets.
  307. **/
  308.  
  309.   if ((pScr = LockPubScreen("Workbench")) == NULL)
  310.           Error("Couldn't lock Workbench screen");
  311.  
  312.   ng.ng_TextAttr = bgIt.ITextFont = pScr->Font;
  313.  
  314.   if ((pVI = GetVisualInfo(pScr, TAG_END)) == NULL ||
  315.       (pG = CreateContext(&pGlist)) == NULL)
  316.           Error("Error from CreateContext");
  317.  
  318.   xOffset = pScr->WBorLeft;
  319.   yOffset = pScr->WBorTop + (WORD) pScr->RastPort.TxHeight + 1;
  320.  
  321. /**
  322.  | Menus
  323. **/
  324.  
  325.   if ((pMenu = CreateMenus(nm, TAG_DONE)) == NULL   ||
  326.       LayoutMenus(pMenu, pVI, TAG_DONE)   == NULL)    {
  327.     Error("Error from Create/LayoutMenus");
  328.   }
  329.  
  330. /**
  331.  | Gadtools button gadgets
  332. **/
  333.  
  334.   ng.ng_LeftEdge += xOffset;
  335.   ng.ng_TopEdge  += yOffset;
  336.   ng.ng_VisualInfo = pVI;
  337.  
  338.   for (i=0; i<NUM_BUT; i++) {
  339.     ng.ng_GadgetText = gadText[i];
  340.     ng.ng_GadgetID   = gadID[i];
  341.     pG = CreateGadget(BUTTON_KIND, pG, &ng,
  342.                       GA_Disabled,  disabled[i],
  343.                       TAG_DONE);
  344.     ng.ng_LeftEdge += (i == 3 ? GAD_DX2 : GAD_DX1);
  345.   }
  346.  
  347. /**
  348.  | Gadtools checkbutton gadget
  349. **/
  350.  
  351.   ng.ng_LeftEdge   = xOffset + CBG_LEFT;
  352.   ng.ng_TopEdge    = yOffset + CBG_TOP;
  353.   ng.ng_Width      = CBG_WIDTH;
  354.   ng.ng_Height     = CBG_HEIGHT;
  355.   ng.ng_GadgetText = "List file names";
  356.   ng.ng_GadgetID   = BUT_FN;
  357.   ng.ng_Flags      = PLACETEXT_RIGHT;
  358.   pG = CreateGadget(CHECKBOX_KIND, pG, &ng,
  359.                     GTCB_Checked,   TRUE,
  360.                     TAG_DONE);
  361.  
  362. /**
  363.  | Gadtools scroller gadget
  364. **/
  365.  
  366.   ng.ng_LeftEdge   = xOffset + SCR_LEFT;
  367.   ng.ng_TopEdge    = yOffset + SCR_TOP;
  368.   ng.ng_Width      = SCR_WIDTH;
  369.   ng.ng_Height     = SCR_HEIGHT;
  370.   ng.ng_GadgetText = NULL;
  371.   ng.ng_GadgetID   = SCROLLER;
  372.   ng.ng_Flags      = 0;
  373.  
  374.   if ((pScroller = pG = CreateGadget(SCROLLER_KIND, pG, &ng,
  375.                         GTSC_Arrows,   ARROW_HEIGHT,
  376.                         PGA_Freedom,   LORIENT_VERT,
  377.                         GA_Immediate,  TRUE,
  378.                         GA_RelVerify,  TRUE,
  379.                         TAG_DONE)) == NULL) {
  380.     Error("Error from CreateGadget");
  381.   }
  382.  
  383. /**
  384.  | The "break window" and its button gadget: computes their location
  385.  | and dimensions using the width and height of the gadget text.
  386. **/
  387.  
  388.   if ((pG = CreateContext(&pBGad)) == NULL) {
  389.     Error("Error from CreateContext (2)");
  390.   }
  391.  
  392.   ng.ng_Width       = (WORD) (IntuiTextLength(&bgIt) + 2 * BKT_XOFFSET);
  393.   ng.ng_Height      = (WORD) (pScr->Font->ta_YSize   + 2 * BKT_YOFFSET);
  394.   ng.ng_LeftEdge    = xOffset + BKT_XOFFSET;
  395.   ng.ng_TopEdge     = yOffset + BKT_YOFFSET;
  396.   ng.ng_GadgetText  = bgText;
  397.   ng.ng_GadgetID    = BUT_BREAK;
  398.   ng.ng_Flags       = PLACETEXT_IN;
  399.  
  400.   if ((pG = CreateGadget(BUTTON_KIND, pG, &ng,
  401.                          TAG_DONE)) == NULL) {
  402.     Error("Error from Creategadget (2)");
  403.   }
  404.  
  405.   bkwWidth  = ng.ng_Width  + 2 * BKT_XOFFSET + xOffset + pScr->WBorRight;
  406.   bkwHeight = ng.ng_Height + 2 * BKT_YOFFSET + yOffset + pScr->WBorBottom;
  407.   bkwLeft   = BEVEL_LEFT + BEVEL_WIDTH  - bkwWidth  + xOffset;
  408.   bkwTop    = BEVEL_TOP  + BEVEL_HEIGHT - bkwHeight + yOffset;
  409.  
  410. /**
  411.  | Output window
  412. **/
  413.  
  414.   zoomBox[0] += xOffset;
  415.   zoomBox[1] += yOffset;
  416.   zoomBox[3]  = yOffset;
  417.  
  418.   if ((pWind = OpenWindowTags(NULL,
  419.                               WA_Left,           WIN_LEFT,
  420.                               WA_Top,            WIN_TOP,
  421.                               WA_Width,          WIN_WIDTH  + xOffset,
  422.                               WA_Height,         WIN_HEIGHT + yOffset,
  423.                               WA_Activate,       TRUE,
  424.                               WA_DragBar,        TRUE,
  425.                               WA_DepthGadget,    TRUE,
  426.                               WA_Zoom,           zoomBox,
  427.                               WA_SmartRefresh,   TRUE,
  428.                               WA_IDCMP,          WIN_IDCMP,
  429.                               WA_Title,          PROG_NAME " v" REVISION,
  430.                               WA_Gadgets,        pGlist,
  431.                               WA_PubScreen,      pScr,
  432.                               TAG_DONE)) == NULL) {
  433.     Error("Error from OpenWindow");
  434.   }
  435.  
  436.   (void) SetMenuStrip(pWind, pMenu);
  437.   GT_RefreshWindow(pWind, NULL);
  438. }
  439.